# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=pastix
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.2.0
pkgrel=2
pkgdesc='High performance parallel solver for very large sparse linear systems based on direct methods (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-hwloc"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-scotch"
	 "${MINGW_PACKAGE_PREFIX}-starpu"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake")
provides=('spm')
license=('LGPL')
url="https://gitlab.inria.fr/solverstack/pastix"
source=("https://gitlab.inria.fr/solverstack/pastix//uploads/5c09cf4e0c120c45e27bc20d24e7f521/${_realname}-${pkgver}.tar.gz"
        "metis-fix.patch")
sha256sums=('DBF2F252C8162CBDE2C52730325B9AE6D8410772BE5B4E93C368D9B8D2D79257'
	    '8B20C71F9AE593003BD40E83F749385776FACDF0C154A127CF2078C5BFB92956')
	    
prepare() {  
  cd "${srcdir}/${_realname}-${pkgver}"
  # https://gitlab.inria.fr/solverstack/pastix/-/commit/635c3af33eea35cb2657a8dc7f2468ab7b34a907
  # https://gitlab.inria.fr/solverstack/morse_cmake/-/commit/d9827910f037555b3ce25fe0a60734c30a86a844
  patch -Np1 -i ${srcdir}/metis-fix.patch
}

build() {
  local _arch_opt=""
  if [ "${CARCH}" = "i686" ]; then
    _arch_opt="-DBUILD_64bits=OFF"
  fi

  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DPASTIX_INT64=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DPASTIX_WITH_PARSEC=OFF \
      -DPASTIX_WITH_STARPU=ON \
      -DPASTIX_ORDERING_METIS=ON \
      -DPASTIX_WITH_MPI=OFF \
      -DBUILD_DOCUMENTATION=OFF \
      -DBUILD_TESTING=OFF \
      ${_arch_opt} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DPASTIX_INT64=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DPASTIX_WITH_PARSEC=OFF \
      -DPASTIX_WITH_STARPU=ON \
      -DPASTIX_ORDERING_PTSCOTCH=ON \
      -DPASTIX_ORDERING_METIS=ON \
      -DPASTIX_WITH_MPI=OFF \
      -DBUILD_DOCUMENTATION=OFF \
      -DBUILD_TESTING=OFF \
      -DBUILD_SHARED_LIBS=ON \
      ${_arch_opt} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install

  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE  
  
  #Remove unwanted examples files
  #rm -rf ${pkgdir}${MINGW_PREFIX}/examples
}
